package grape.crm.service.customer.impl;

import grape.common.exception.runtime.InvalidParamsException;
import grape.common.exception.runtime.RBaseException;
import grape.common.service.common.BaseServiceImpl;
import grape.crm.service.customer.api.ICustomerService;
import grape.crm.service.customer.map.CustomerServiceMapper;
import grape.crm.service.customer.mapper.CustomerMapper;
import grape.crm.service.customer.param.AddCustomerParam;
import grape.crm.service.customer.po.Customer;
import grape.crm.service.customerassociatedcontactperson.api.ICustomerAssociatedContactPersonService;
import grape.crm.service.customerassociatedcontactperson.map.CustomerAssociatedContactPersonServiceMapper;
import grape.crm.service.customerassociatedcontactperson.param.AddCustomerAssociatedContactPersonParam;
import grape.crm.service.customerassociatedcontactperson.po.CustomerAssociatedContactPerson;
import grape.crm.service.customerbelong.api.ICustomerBelongService;
import grape.crm.service.customerbelong.po.CustomerBelong;
import grape.crm.service.customercontact.api.ICustomerContactService;
import grape.crm.service.customercontact.map.CustomerContactServiceMapper;
import grape.crm.service.customercontact.param.AddCustomerContactParam;
import grape.crm.service.customercontact.po.CustomerContact;
import grape.crm.service.customercredential.api.ICustomerCredentialService;
import grape.crm.service.customercredential.map.CustomerCredentialServiceMapper;
import grape.crm.service.customercredential.param.AddCustomerCredentialParam;
import grape.crm.service.customercredential.po.CustomerCredential;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 客户表 服务实现类
 * </p>
 *
 * @author yangwei
 * @since 2020-09-14
 */
@Service
public class CustomerServiceImpl extends BaseServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Autowired
    private CustomerServiceMapper customerServiceMapper;
    @Autowired
    private CustomerContactServiceMapper customerContactServiceMapper;
    @Autowired
    private CustomerCredentialServiceMapper customerCredentialServiceMapper;
    @Autowired
    private ICustomerBelongService iCustomerBelongService;
    @Autowired
    private ICustomerContactService iCustomerContactService;
    @Autowired
    private ICustomerCredentialService iCustomerCredentialService;

    @Autowired
    private ICustomerAssociatedContactPersonService iCustomerAssociatedContactPersonService;
    @Autowired
    private CustomerAssociatedContactPersonServiceMapper customerAssociatedContactPersonServiceMapper;

    @Override
    public Customer addCustomer(AddCustomerParam addCustomerParam) {
        // code 唯一检查
        if (existCode(addCustomerParam.getCode())) {
            throw new InvalidParamsException("编码已存在");
        }
        Customer customer = customerServiceMapper.paramToPo(addCustomerParam);
        customer.setIsBlack(false);
        // 保存用户
        customer = insert(customer);
        // 联系方式
        List<AddCustomerContactParam> customerContacts = addCustomerParam.getCustomerContacts();
        if (!isEmpty(customerContacts)) {
            iCustomerContactService.batchAddCustomerContact(customer.getId(),customerContacts);
        }
        // 证件
        List<AddCustomerCredentialParam> customerCredentials = addCustomerParam.getCustomerCredentials();
        if (!isEmpty(customerCredentials)) {
            // 保存联系方式
            iCustomerCredentialService.batchAddCustomerCredential(customer.getId(),customerCredentials);
        }
        // 关联联系人
        List<AddCustomerAssociatedContactPersonParam> customerAssociatedContactPersons = addCustomerParam.getCustomerAssociatedContactPersons();
        if (!isEmpty(customerAssociatedContactPersons)) {
            // 保存关联联系人
            iCustomerAssociatedContactPersonService.batchAddCustomerAssociatedContactPerson(customer.getId(),customerAssociatedContactPersons);
        }
        // 归属
        CustomerBelong customerBelong = new CustomerBelong();
        customerBelong.setCustomerId(customer.getId());
        customerBelong.setUserId(addCustomerParam.getBelongUserId());
        iCustomerBelongService.insert(customerBelong);
        return customer;
    }
}
